<template>
  <div>
    <el-upload
      list-type="picture-card"
      :limit="1"
      action="#"
      :on-preview="preview"
      :on-remove="handleRemove"
      :on-change="changeFile"
      :file-list="fileList"
      :class="{ disabled: fileComputed }"
      :before-upload="beforeUpload"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="showPercent" :percentage="percent" style="width: 180px" />
    <el-dialog v-if="showDialog" :visible.sync="showDialog" title="图片预览">
      <img :src="imgUrl" alt="" style="width:100%">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKID480d06weDbmkV3NUOBpRpysW3Wr0uaz7',
  SecretKey: 'uYv1qPhiSw96mCMFrEMDn68tOcZFhurX'
})
export default {
  data() {
    return {
      fileList: [], // 预览图
      showDialog: false, // 弹窗是否显示
      imgUrl: '',
      currentFileUid: null, // 正在上传的uid
      percent: 0, // 进度条百分比
      showPercent: false // 进度条是否显示
    }
  },
  computed: {
    // 有图就隐藏+
    fileComputed() {
      return this.fileList.length === 1
    }
  },
  methods: {
    // 点击预览
    preview(file) {
      this.imgUrl = file.url
      this.showDialog = true
    },
    // 删除
    handleRemove(file, fileList) {
      this.fileList = fileList // 删除后的数据
    },
    // 上传
    changeFile(file) {
      if (!this.fileList.some(item => item.uid === file.uid)) {
        this.fileList.push(file)
      }
    },
    // 上传图片检查
    beforeUpload(file) {
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      // 类型检查
      if (!types.some(item => item === file.type)) {
        this.$message.error('上传图片只能是 JPG、GIF、BMP、PNG 格式!')
        return false
      }
      // 文件大小检查
      const maxSize = 5 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.error('上传图片不能大于5M!')
        return false
      }
      this.currentFileUid = file.uid
      // 开启进度条
      this.showPercent = true
      return true
    },
    // 上传
    upload(params) {
      if (params.file) {
        cos.putObject({
          Bucket: 'aliou-1311254211', // 存储桶
          Region: 'ap-shanghai', // 地域
          Key: params.file.name, // 文件名
          Body: params.file, // 上传对象
          StorageClass: 'STANDARD', // 标准模式上传
          // 进度条推进触发
          onProgress: (params) => {
            this.percent = params.percent * 100
          }
        }, (err, data) => {
          console.log(err || data)
          // 上传成功
          if (!err && data.statusCode === 200) {
            // 回写fileList
            this.fileList = this.fileList.map(item => {
              // 成功
              if (item.uid === this.currentFileUid) {
                return { url: `http://${data.Location}`, upload: true } // upload成功标记
              }
              return item
            })
            // 关闭进度条
            setTimeout(() => {
              this.showPercent = false
              this.percent = 0
            }, 1000)
          }
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
/* /deep/的scss写法 */
::v-deep .disabled .el-upload--picture-card{
   display: none;
}
</style>
